﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GsnHelper;
using ZhongheCms.Models;

namespace ZhongheCms.Controllers
{
    public class ProInfoController : Controller
    {
        public ActionResult Index(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var qry = GswTblProInfo.GetProList(keywords, int.Parse(fid), GswTblUser.CurrentUser.CustomerId);
            return View(qry);
        }
        public ActionResult Create(int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            var pro = id == 0
                ? new GswTblProInfo { CustomerId = GswTblUser.CurrentUser.CustomerId, ProNo = 1 } : GswTblProInfo.SingleOrDefault(id);
            if (id == 0)
            {
                var qry =
               GswTblProInfo.FirstOrDefault("select max(ProNo) as ProNo from GswTblProInfo where CustomerId=" +
                                            GswTblUser.CurrentUser.CustomerId + " and FunctionId=" + fid);
                if (qry != null)
                    pro.ProNo = qry.ProNo + 1;
            }
            if (GswTblUser.CurrentUser.CustomerId == 2)
            {
                var appid = 6;
                if (!string.IsNullOrEmpty(p))
                {
                    var funs = GswTblFunction.SingleOrDefault("where FuncName='应用领域' and ActionParam='" + p + "'");
                    if (funs != null)
                    {
                        appid = funs.Id;
                    }
                }
                pro.ProApp =
                         GswTblContent.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                              " and FunctionId=" + appid + " order by ContentNo");
            }
            if (GswTblUser.CurrentUser.CustomerId == 15)
            {
                var appid = 54;
                if (!string.IsNullOrEmpty(p))
                {
                    var funs = GswTblFunction.SingleOrDefault("where FuncName='产品标签' and ActionParam='" + p + "'");
                    if (funs != null)
                    {
                        appid = funs.Id;
                    }
                }
                pro.ProApp =
                         GswTblContent.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                              " and FunctionId=" + appid + " order by ContentNo");
            }
            pro.ProLists = GswTblProList.Fetch("where ProId=" + id);
            pro.ProTypes = GswTblProInfo.GetProTypes(GswTblUser.CurrentUser.CustomerId,p,fid);

            return View(pro);
        }
        [HttpPost]
        [ValidateInput(false)]
        public ActionResult Create(FormCollection c, int id = 0, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;
            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            if (InputValid.StrToInt(c["ProTypeId"], 0) == 0)
                ModelState.AddModelError(string.Empty, "请选择产品类别");
            var pro= id == 0 ? new GswTblProInfo() : GswTblProInfo.SingleOrDefault(id);
            pro.ProName = c["ProName"];
            pro.ProNo = InputValid.StrToInt(c["ProNo"], 0);
            pro.NewFlag = InputValid.StrToInt(c["NewFlag"], 0);
            pro.ProBrief = c["ProBrief"];
            pro.ProDesc = c["ProDesc"];
            pro.ProPic = c["ProPic"];
            pro.FunctionId = int.Parse(fid);
            pro.CustomerId = GswTblUser.CurrentUser.CustomerId;
            pro.ProTypeId = InputValid.StrToInt(c["ProTypeId"], 0);
            pro.ModelNo = c["ModelNo"];
            if (GswTblUser.CurrentUser.CustomerId == 2)
            {
                var appid = 6;
                if (!string.IsNullOrEmpty(p))
                {
                    var funs = GswTblFunction.SingleOrDefault("where FuncName='应用领域' and ActionParam='" + p + "'");
                    if (funs != null)
                    {
                        appid = funs.Id;
                    }
                }
                pro.ProApp =
                         GswTblContent.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                              " and FunctionId=" + appid + " order by ContentNo");
            }
            if (GswTblUser.CurrentUser.CustomerId == 15)
            {
                var appid = 54;
                if (!string.IsNullOrEmpty(p))
                {
                    var funs = GswTblFunction.SingleOrDefault("where FuncName='产品标签' and ActionParam='" + p + "'");
                    if (funs != null)
                    {
                        appid = funs.Id;
                    }
                }
                pro.ProApp =
                         GswTblContent.Fetch("where CustomerId=" + GswTblUser.CurrentUser.CustomerId +
                                              " and FunctionId=" + appid + " order by ContentNo");
            }
          
            int myId = pro.Id;
            try
            {
                if (ModelState.IsValid)
                {

                    if (id == 0)
                    {
                        pro.Insert();
                        myId = pro.Id;
                        pro.ProName = "";
                        pro.ProNo = InputValid.StrToInt(c["ProNo"], 0)+1;
                        pro.NewFlag = 0;
                        pro.ProBrief ="";
                        pro.ProDesc = "";
                        pro.ProPic = "";
                        pro.Id = 0;
                    }
                    else
                    {
                        pro.Update();
                     
                    }
                    if (pro.ProApp!=null)
                    {
                        GswTblProList.Delete("where ProId=" + myId);
                        foreach (var appList in from item in pro.ProApp where !string.IsNullOrEmpty(c["chk" + item.Id]) select new GswTblProList
                        {
                            ProAppId = item.Id,
                            ProId = myId

                        })
                        {
                            appList.Insert();
                        }
                    }
                    ViewBag.scode = "1";
                }

            }
            catch (Exception e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
            }
            if (!ModelState.IsValid)
                ViewBag.scode = "0";
            pro.ProTypes = GswTblProInfo.GetProTypes(GswTblUser.CurrentUser.CustomerId,p,fid);
           
            pro.ProLists = GswTblProList.Fetch("where ProId=" + id);
            return View(pro);

        }

        public ActionResult Delete(int id, string m = "", string f = "", string p = "", string fid = "", string keywords = "")
        {
            ViewBag.m = m;
            ViewBag.f = f;
            ViewBag.p = p;
            ViewBag.fid = fid;
            ViewBag.keywords = keywords;

            if (GswTblUser.CurrentUser == null)
                return RedirectToAction("Login", "Account");
            GswTblUser.CurrentUser.ModuleName = m;
            GswTblUser.CurrentUser.FuncName = f;
            GswTblProInfo.Delete(id);
            var qry = GswTblProInfo.GetProList(keywords,int.Parse(fid), GswTblUser.CurrentUser.CustomerId);
            return View("Index", qry);
        }


    }
}
